package com.hcmiu.buddytracking;

import org.json.JSONException;
import org.json.JSONObject;

import setMethods.setPOI;
import Utils.toastMaking;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class AddPOI extends Activity {
	private String PREF_NAME = "buddytracking";
	private String currentPhoneNumber;
	private SharedPreferences pref;
	private EditText POITitle;
	private EditText POILatitude;
	private EditText POILongitude;
	private EditText POIDescription;
	private CheckBox checkbox;
	private String ischeckedBox;
	private Button markButton;
	private Button cancelButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.addpoi);
		pref = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
		currentPhoneNumber = pref.getString("currentPhoneNumber", "");
		POITitle = (EditText) findViewById(R.id.POITitle);
		POILatitude = (EditText) findViewById(R.id.poiLatitude);
		POILongitude = (EditText) findViewById(R.id.POILongitude);
		POIDescription = (EditText) findViewById(R.id.POIDescription);
		markButton = (Button) findViewById(R.id.markButton);
		cancelButton = (Button) findViewById(R.id.cancelMarkButton);
		checkbox = (CheckBox) findViewById(R.id.POICheckBox);
		ischeckedBox = "0";

		POILatitude.setText(Double
				.toString(pref.getInt("POILatitude", 0) / 1E6));
		POILongitude.setText(Double
				.toString(pref.getInt("POILongitude", 0) / 1E6));

		markButton.setOnClickListener(saveButtonListener());
		cancelButton.setOnClickListener(cancelBuClickListener());
		checkbox.setOnCheckedChangeListener(checkHidden());

	}

	private void AddNewPOI(String currentPhoneNumber, String title,
			String latitude, String longitude, String description,
			String isHidden) throws JSONException {
		JSONObject object = new JSONObject();

		object.put("currentPhoneNumber", currentPhoneNumber);
		object.put("title", title);
		object.put("latitude", latitude);
		object.put("longitude", longitude);
		object.put("description", description);
		object.put("isHidden", isHidden);
		setPOI.process(object.toString());

	}

	private View.OnClickListener saveButtonListener() {
		return (new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String titleText = POITitle.getText().toString();
				String desText = POIDescription.getText().toString();
				if (titleText.length() == 0 || desText.length() == 0) {
					toastMaking.errorToast(getApplicationContext(),
							"Enter POI Title and Description").show();
				} else if (titleText.length() > 20) {
					toastMaking.errorToast(getApplicationContext(),
							"Shorten your title - max 20 chars").show();
				} else if (desText.length() > 100) {
					toastMaking.errorToast(getApplicationContext(),
							"Shorten your description - max 100 chars").show();
				} else if (titleText.length() != 0 && desText.length() != 0) {
					try {
						AddNewPOI(currentPhoneNumber, titleText, POILatitude
								.getText().toString(), POILongitude.getText()
								.toString(), desText, ischeckedBox);
						pref.edit().putString("POITitle", titleText).putString(
								"POIDescription", desText).commit();
						setResult(RESULT_OK, null);
						finish();
						AddPOI.this.finish();
					} catch (JSONException e) {
						e.printStackTrace();
					}

				}

			}
		});
	}

	private View.OnClickListener cancelBuClickListener() {
		return (new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				AddPOI.this.finish();
			}
		});
	}

	private OnCheckedChangeListener checkHidden() {
		return (new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (ischeckedBox.equals("0"))
					ischeckedBox = "1";
				else if (ischeckedBox.equals("1"))
					ischeckedBox = "0";
			}
		});
	}

}